package com.itcast.donkey.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.listview.PinnedSectionListView;
import com.itcast.donkey.modles.DetailedJourneyModles;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

/**
 * Created by lanouhn on 16/9/1.
 */
public class TextListViewAdapter extends ArrayAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private Context context;
    private List<Map<String, Object>> modles;
    private TextListRecyclerAdapter adapter;
    private String title;
    private DetailedJourneyModles.PlanDaysBean.PlanNodesBean daysBean;

    public TextListViewAdapter(Context context, int resource, List<Map<String, Object>> modles) {
        super(context, resource);
        this.context = context;
        this.modles = modles;
    }


    @Override
    public int getCount() {
        return modles.size();
    }

    @Override
    public Object getItem(int position) {
        return modles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        switch (type) {

            case 0:
                //标题
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.WHITE);
                textView.setTextSize(18);
                textView.setPadding(10, 0, 0, 0);
                textView.setTextColor(Color.BLACK);
                textView.setText("DAY" + 1 + "  " + (
                        (DetailedJourneyModles.PlanDaysBean) modles.get(position)
                                .get("0")).getPlan_nodes().get(0)
                        .getDestination().getName_zh_cn());
                title = ((DetailedJourneyModles.PlanDaysBean) modles.get(position)
                        .get("0")).getMemo();
                convertView = textView;
                break;
            case 1:
                //内容
                daysBean = ((DetailedJourneyModles.PlanDaysBean.PlanNodesBean) modles.get(position).get("1"));

                View view = View.inflate(context, R.layout.item_recycler_textlistview, null);
                LinearLayout linTitle = (LinearLayout) view.findViewById(R.id.lin_title);
                LinearLayout lineItemTextlistClick = (LinearLayout) view.findViewById(R.id.line_item_textlist_click);
                TextView tvItemTextlistTitle = (TextView) view.findViewById(R.id.tv_item_textlist_title);
                ImageView ivItemTextlistImage = (ImageView) view.findViewById(R.id.iv_item_textlist_image);
                TextView tvItemTextlistContent = (TextView) view.findViewById(R.id.tv_item_textlist_content);
                if (daysBean.isCandidate()) {
                    lineItemTextlistClick.setVisibility(View.VISIBLE);
                    tvItemTextlistContent.setVisibility(View.VISIBLE);
                    tvItemTextlistTitle.setText("备选" + daysBean.getEntry_name());
                    tvItemTextlistContent.setText(daysBean.getTips());
                    ivItemTextlistImage.setVisibility(View.GONE);
                } else {
                    lineItemTextlistClick.setVisibility(View.VISIBLE);
                    tvItemTextlistContent.setVisibility(View.VISIBLE);
                    int x = daysBean.getPosition() + 1;
                    tvItemTextlistTitle.setText("第" + x + "站:" + daysBean.getEntry_name());
                    Picasso.with(context).load(daysBean.getImage_url()).into(ivItemTextlistImage);
                    tvItemTextlistContent.setText(daysBean.getTips());
                }
                convertView = view;
                break;
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {

        Map<String, Object> map = modles.get(position);
        String s = map.keySet().iterator().next();
        return Integer.parseInt(s);
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        boolean type = false;
        switch (viewType) {
            case 1:
                type = false;
                break;
            case 0:
                type = true;
                break;
            default:
                type = false;
                break;
        }
        return type;
    }
}
